<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>管理</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../assets/module/admin.css?v=315"/>
</head>
<body>

<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <table class="layui-table" id="tableBook" lay-filter="tableBook"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarBook">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">屏蔽</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script type="text/javascript" src="../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../assets/js/common.js?v=315"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'tableX','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var tableX = layui.tableX;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableBook',
            url: '/api/getAllComments',
            page: true,
            height: "full-70",
            toolbar: false,
            headers : {
                Authorization: '[[${session.token}]]'
            },
            cellMinWidth: 100,
            cols: [
                [
                    {title: '#',type:'numbers'}
                    ,{field: 'comment', sort: true,title: '评论',minWidth:200}
                    ,{field: 'email', sort: true,title: '邮箱'}
                    ,{field: 'nick', sort: true,title: '昵称',minWidth:200}
                    ,{field: 'create_time', sort: true, title:'评论时间',templet: function (d) {
                        return util.toDateString(d.create_time);
                    }}
                    ,{field: 'blog', sort: true,title: '博客',templet: function (d) {return d.boke.title},minWidth:200}
                    ,{align: 'center', toolbar: '#tableBarBook', title: '操作', minWidth: 200}
                ]
            ]
        });

        // 工具条点击事件
        table.on('tool(tableBook)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                doEdit(data.id, data.comment);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.comment);
            }
        });

        function doDel(id, name) {
            layer.confirm('确定要删除“' + name + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.ajax({
                    url:'/api/delComment',
                    type:"GET",
                    data:{
                        id: id
                    },
                    headers : { Authorization: '[[${session.token}]]'},
                    dataType:"json",
                    success: function(res){
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({}, 'data');
                        } else {
                            layer.msg("权限不够", {icon: 2});
                        }
                    },
                    error : function(xhr,textStatus,errorThrown){
                        if (xhr.status == 401) {
                            layer.closeAll('loading');
                            layer.msg("权限不够", {icon: 2});
                        } else{
                            // 调用外部的error
                            error && error(xhr,textStatus,errorThrown);
                        }
                    }
                })
            });
        }

        function doEdit(id, name) {
            layer.confirm('确定要屏蔽“' + name + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.ajax({
                    url:'/api/editComment',
                    type:"GET",
                    data:{
                        id: id
                    },
                    headers : { Authorization: '[[${session.token}]]'},
                    dataType:"json",
                    success: function(res){
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({}, 'data');
                        } else {
                            layer.msg("权限不够", {icon: 2});
                        }
                    },
                    error : function(xhr,textStatus,errorThrown){
                        if (xhr.status == 401) {
                            layer.closeAll('loading');
                            layer.msg("权限不够", {icon: 2});
                        } else{
                            // 调用外部的error
                            error && error(xhr,textStatus,errorThrown);
                        }
                    }
                })
            });
        }
    });
</script>

</body>
</html>